package com.mrent.web.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrent.entity.AdminPayment;
import com.mrent.entity.ManagerPayment;
import com.mrent.service.AdminPaymentService;
import com.mrent.service.ManagerPaymentService;
import com.mrent.service.Page;
import com.mrent.util.Constance;
import com.mrent.util.RenderUtil;

@Controller
public class AdminPaymentController {
	
	@Autowired
	private AdminPaymentService adminPaymentService;
	
	@Autowired
	private ManagerPaymentService managerPaymentService;
	

	
	/**
	 * 支付管理.
	 * 
	 * @return
	 */
	@RequestMapping("/admin/payment/index")
	public String index(){
		
		return "admin/payment/home";
	}
	
	
	
	/**
	 * 支付列表.
	 * @return
	 */
	@RequestMapping("/admin/payment/list")
	public String list(String page,HttpServletResponse response){
		int p = 1;
		Page pp = new Page();
		try{
			p = Integer.parseInt(page);
		}catch(Exception e){
			p = 1;
		}
		pp.setPageNo(p);
		pp.setPageSize(Constance.DEFAULT_PAGE_SIZE);
		
		List<AdminPayment> list = adminPaymentService.listPayment(pp);
		
		Map<String,Object> json = new HashMap<String,Object>();
		json.put("rows", list);
		json.put("total", pp.getTotalCount());
		RenderUtil.renderJson(json,response);
		
		return null;
	}
	
	/**
	 * 添加支付方式.
	 * 
	 * @return
	 */
	@RequestMapping("/admin/payment/add")
	public String add(){
		
		return "admin/payment/edit";
	}
	
	/**
	 * 修改支付方式.
	 * 
	 * @param payId
	 * @param model
	 * @return
	 */
	@RequestMapping("/admin/payment/modify/{payId}")
	public String modify(@PathVariable Long payId,ModelMap model){
		if(payId!=null){
			AdminPayment payment = adminPaymentService.getPayment(payId);
			model.addAttribute("payment", payment);
		}
		return "/admin/payment/edit";
	}
	
	/**
	 * 保存支付方式.
	 * 
	 * @param adminPayment
	 * @param response
	 * @return
	 */
	@RequestMapping("/admin/payment/save")
	public String save(AdminPayment adminPayment,HttpServletResponse response){
		if(adminPayment.getPayId() != null && adminPayment.getPayId() >0){
			adminPayment.setEnable(new Integer(1));
			adminPaymentService.updatePayment(adminPayment);
		}else{
			adminPayment.setEnable(new Integer(1));
			adminPaymentService.savePayment(adminPayment);
		}
		Map<String,Object> json = new HashMap<String,Object>();
		json.put("result", "Success");
		RenderUtil.renderJson(json, response);
		
		return null;
	}
	
	/**
	 * 删除支付方式.
	 * 
	 * @return
	 */
	@RequestMapping("/admin/payment/del/{payId}")
	public String del(@PathVariable Long payId,HttpServletResponse response){
		if(payId != null){
			adminPaymentService.delPayment(payId);
			List<ManagerPayment> mplist = managerPaymentService.listByPayId(payId);
			if(mplist.size() > 0){
				for(ManagerPayment mpay:mplist){
					managerPaymentService.delPayment(mpay.getId());
				}
			}
		}
		Map<String,Object> json = new HashMap<String,Object>();
		json.put("result", "Success");
		RenderUtil.renderJson(json, response);
		return null;
	}
	
	
}
